<?php

namespace App\Http\Controllers\Common\Traits;
use App\Models\File;

trait FormatResponseTrait
{
    /**
     * 响应基础方法
     * @param array $data
     * @param int $status
     * @return \Illuminate\Http\JsonResponse
     * User: john_chu
     * Email: john1668@qq.com
     */
    public function base($data, $status)
    {
        return response()->json($data)->setStatusCode($status);
    }

    /**
     * 响应一个错误信息
     * @param string $message
     * @param int $status
     * @return \Illuminate\Http\JsonResponse
     * User: john_chu
     * Email: john1668@qq.com
     * @
     */
    public function error($message, $status = 400)
    {
       return $this->base([
            'code'    => $status,
            'message' => $message,
        ], 200);
    }

    /**
     * 响应一个成功消息
     * @param $data
     * @param int $status
     * @return \Illuminate\Http\JsonResponse
     * User: john_chu
     * Email: john1668@qq.com
     */
    public function success($message = '', $data = [], $status = 200)
    {
        return $this->base([
            'code'  => $status,
            'message' => $message,
            'data' => $data
        ], $status);
    }

    /**
     * @param array $data
     * @param int $status
     * @return \Illuminate\Http\JsonResponse
     * User: john_chu
     * Email: john1668@qq.com
     */
    public function resource($data, $status = 200)
    {
        return response()->json($data, $status);
    }

    /**
     * 获取客户端真实ip
     * @return mixed
     */
    protected function _getRealIpAddr() {
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            $ip=$_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //to check ip is pass from proxy
            $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip=$_SERVER['REMOTE_ADDR'];
        }

        return $ip;
    }






}